package com.xiaoxiong.text;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class GetAllLoad {
	private Context context;
	private String html;
	private SharedPreferences post_message;

	public GetAllLoad(Context context, String html) {
		this.context = context;
		this.html = html;
		post_message = this.context.getSharedPreferences("postload",
				Context.MODE_PRIVATE);
		Deal();
	}

	private void Deal() {

		Document content = Jsoup.parse(html);
		Elements element = content.getElementsByTag("tbody");

		int flag = 0;

		for (Element links : element) {
			if (flag < 6) {
				flag++;
				continue;
			}
			String title = links.html();
			GetLoad(title);
		}
	}

	private void GetLoad(String value) {
		
		if (Matching(value, "个人信息查询")) {

			GetPersonInfo(value);

		} else if (Matching(value, "考试时间查看")) {

			GetExamTime(value);

		} else if (Matching(value, "预交费用查看")) {

			GetTuitionUseList(value);

		} else if (Matching(value, "考勤")) {

			GetAttendance(value);

		} else if (Matching(value, "奖惩记录")) {

			GetAwardAndPunishment(value);

		} else if (Matching(value, "晚归、违规用电记录")) {

			GetViolationRecord(value);

		}
	}

	private boolean Matching(String str, String val) {

		Matcher m1 = Pattern.compile(val).matcher(str);

		if (m1.find()) {
			return true;
		} else {
			return false;
		}
	}

	// 获取个人信息的学生id
	private void GetPersonInfo(String value) {

		StringBuilder sbValue = new StringBuilder(value);
		sbValue.delete(0, sbValue.indexOf("studentid") + 10);
		sbValue.delete(sbValue.indexOf("'"), sbValue.length());

		Editor editor = post_message.edit();
		editor.putString("personinfoid", sbValue.toString());
		editor.commit();
	}

	// 获取考试时间的学生id
	private void GetExamTime(String value) {

		StringBuilder sbValue = new StringBuilder(value);
		sbValue.delete(0, sbValue.indexOf("studentid") + 10);
		sbValue.delete(sbValue.indexOf("'"), sbValue.length());

		Editor editor = post_message.edit();
		editor.putString("examtimeid", sbValue.toString());
		editor.commit();
	}

	// 获取预交费用的学生id
	private void GetTuitionUseList(String value) {

		StringBuilder sbValue = new StringBuilder(value);
		sbValue.delete(0, sbValue.indexOf("studentid") + 10);
		sbValue.delete(sbValue.indexOf("'"), sbValue.length());

		Editor editor = post_message.edit();
		editor.putString("tuitionuselistid", sbValue.toString());
		editor.commit();
	}

	// 获取考勤信息学生id
	private void GetAttendance(String value) {

		StringBuilder sbValue = new StringBuilder(value);
		
		sbValue.delete(0, sbValue.indexOf("studentID"));
		sbValue.delete(sbValue.indexOf("'"), sbValue.length());
		String str = sbValue.toString().replaceAll("amp;", "");
		
		Editor editor = post_message.edit();
		editor.putString("attendanceid", str);
		editor.commit();
	}

	// 获取奖惩记录学生id
	private void GetAwardAndPunishment(String value) {

		StringBuilder sbValue = new StringBuilder(value);
		
		sbValue.delete(0, sbValue.indexOf("stuname"));
		sbValue.delete(sbValue.indexOf("'"), sbValue.length());
		String str = sbValue.toString().replaceAll("amp;", "");

//		Log.d("value", str);
		Editor editor = post_message.edit();
		/**
		 * 查这个类时，用到的是这个
		 */
		editor.putString("awardandpunishment", str);
		editor.commit();
	}
	// 获取晚归、违规用电记录学生id
	private void GetViolationRecord(String value) {

		StringBuilder sbValue = new StringBuilder(value);
		
		sbValue.delete(0, sbValue.indexOf("gzCode"));
		sbValue.delete(sbValue.indexOf("'"), sbValue.length());
		String str = sbValue.toString().replaceAll("amp;", "");
		
//		Log.d("value", str);
		Editor editor = post_message.edit();
		editor.putString("violationrecord", str);
		editor.commit();
	}

}
